﻿$(function () {
    $(".ui.dropdown").dropdown();

    ace.config.set('basePath', "/Scripts/ace-editor");

    var editor1 = ace.edit("editor1");
    editor1.setTheme(getTheme1());
    editor1.getSession().setMode(getMode1());

    var editor2 = ace.edit("editor2");
    editor2.setTheme(getTheme2());
    editor2.getSession().setMode(getMode2());

    $.connection.hub.start();

    var share = $.connection.shareHub;

    share.client.updateCode = function (message1, message2, cursor1, cursor2) {
        editor1.getSession().setValue(message1);
        editor1.selection.moveCursorTo(cursor1.row, cursor1.column + 1);

        editor2.getSession().setValue(message2);
        editor2.selection.moveCursorTo(cursor2.row, cursor2.column + 1);
    };

    $.connection.hub.start().done(function () {
        editor1.on('change', function () {
            if (editor1.curOp && editor1.curOp.command.name) {
                share.server.updateCode(editor1.getSession().getValue(), editor2.getSession().getValue(), editor1.getCursorPosition(), editor2.getCursorPosition());
            };
        });

        editor2.on('change', function () {
            if (editor2.curOp && editor2.curOp.command.name) {
                share.server.updateCode(editor1.getSession().getValue(), editor2.getSession().getValue(), editor1.getCursorPosition(), editor2.getCursorPosition());
            };
        });
    });
});
